﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ServiceBus.Contract;



namespace ServiceBus.Service
{
    public class EventEngine
    {
        #region Events
        public static event EventHandler HeartBeatEvent;
        public static event EventHandler<Message> MessageSending;
        public static event EventHandler<Message> EmailSending;
        public static event EventHandler<Message> DataSaving;
        #endregion

        #region Static Fields
        private static System.Timers.Timer HeartBeatTrigger = null;
        #endregion

        #region Static Constructor
        static EventEngine()
        {
            HeartBeatTrigger = new System.Timers.Timer(2000);
            HeartBeatTrigger.Elapsed += new System.Timers.ElapsedEventHandler(HeartBeatTrigger_Elapsed);
            HeartBeatTrigger.Start();
        }
        #endregion

        #region Static Methods
        static void HeartBeatTrigger_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (HeartBeatEvent != null) HeartBeatEvent(null, null);
        }
        /// <summary>
        /// send email
        /// </summary>
        public static void SendMail(Message message)
        {
            if (EmailSending != null)
            {
                try
                {
                    EmailSending(null, message);
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
            }

        }
        /// <summary>
        /// send wcf message
        /// </summary>
        /// <param name="message"></param>
        public static void SendMessage(Message message)
        {
            if (MessageSending != null)
            {
                try
                {
                    MessageSending(null, message);
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
            }
        }
        /// <summary>
        /// Save message to database
        /// </summary>
        /// <param name="message"></param>
        public static void SaveMessage(Message message)
        {
            if (DataSaving != null)
            {
                try
                {
                    DataSaving(null, message);
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
            }
        }
        #endregion
    }
}
